package com.lucaswangdev.biz.manager.impl;

import com.lucaswangdev.biz.manager.WorkOrderManager;
import com.lucaswangdev.constant.common.ErrorConstants;
import com.lucaswangdev.exception.BusinessException;
import com.lucaswangdev.model.convert.WorkOrderConverter;
import com.lucaswangdev.model.domain.WorkOrderDO;
import com.lucaswangdev.model.domain.WorkOrderParam;
import com.lucaswangdev.model.dto.WorkOrderDTO;
import com.lucaswangdev.model.mapper.WorkOrderMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class WorkOrderManagerImpl implements WorkOrderManager {

    @Resource
    private WorkOrderMapper workOrderMapper;

    @Override
    public Long create(WorkOrderDTO workOrderDTO) {
        WorkOrderDO workOrderDO = WorkOrderConverter.INSTANCE.convertDTO2DO(workOrderDTO);
        Date now = new Date();
        workOrderDO.setGmtCreate(now);
        workOrderDO.setGmtModified(now);
        int effectRow = workOrderMapper.insert(workOrderDO);
        if (effectRow != 1) {
            throw new BusinessException(ErrorConstants.CREATE_ERROR.getErrorCode(), ErrorConstants.CREATE_ERROR.getErrorMsg());
        }
        return workOrderDO.getId();
    }

    @Override
    public Long countByParam(WorkOrderParam param) {
        return workOrderMapper.countByParam(param);
    }

    @Override
    public List<WorkOrderDTO> queryByParam(WorkOrderParam param) {
        List<WorkOrderDTO> workOrderDTOS = new ArrayList<>();
        List<WorkOrderDO> workOrderDOS = workOrderMapper.selectByParam(param);
        if (CollectionUtils.isNotEmpty(workOrderDOS)) {
            workOrderDOS.forEach(workOrderDO -> {
                workOrderDTOS.add(WorkOrderConverter.INSTANCE.convertDO2DTO(workOrderDO));
            });
        }
        return workOrderDTOS;
    }

    @Override
    public void update(WorkOrderDTO workOrderDTO) {
        WorkOrderDO workOrderDO = WorkOrderConverter.INSTANCE.convertDTO2DO(workOrderDTO);
        Date now = new Date();
        workOrderDO.setGmtModified(now);
        int effectRow = workOrderMapper.updateByPrimaryKeySelective(workOrderDO);
        if (effectRow != 1) {
            throw new BusinessException(ErrorConstants.UPDATE_ERROR.getErrorCode(), ErrorConstants.UPDATE_ERROR.getErrorMsg());
        }
    }

    @Override
    public void deleteById(Long id) {
        int effectRow = workOrderMapper.deleteByPrimaryKey(id);
        if (effectRow != 1) {
            throw new BusinessException(ErrorConstants.DELETE_ERROR);
        }
    }

    @Override
    public WorkOrderDTO queryById(Long id) {
        WorkOrderDO workOrderDO = workOrderMapper.selectByPrimaryKey(id);
        if (workOrderDO == null) {
            throw new BusinessException(ErrorConstants.NOT_FOUND_BY_ID, "id: " + id);
        }
        return WorkOrderConverter.INSTANCE.convertDO2DTO(workOrderDO);
    }
}
